//
//  XSJStatusOriginalView.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/14.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
import SnapKit
class XSJStatusOriginalView: UIView {
    
    var originalViewBottom : Constraint?
    
    var statusViewModel: XSJStatusViewModel? {
        didSet {
            
            if let url = statusViewModel?.status?.user?.profile_image_url  {
                userImageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
//            contentLabel.text = statusViewModel?.status?.text

            contentLabel.attributedText = statusViewModel?.origianlAttributedString

            contentLabel.numberOfLines = 0
//            timeLabel.text = "刚刚"
            timeLabel.text = statusViewModel?.timeContent
//            sourceLabel.text = "微博"
            sourceLabel.text = statusViewModel?.sourceContent
            
            originalViewBottom?.uninstall()
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
                
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.originalViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
            }else{
            
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
                
            }
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            
        }
    }
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    
    //  微博内容
    private lazy var contentLabel: UILabel = {
        let lable =  UILabel(textColor: UIColor.darkGrayColor(), fontSize: HomeFontSize)
        lable.numberOfLines = 0
        return lable
    }()
    
    private lazy var pictureView : XSJStatusPictureView = {
    
        let view = XSJStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        contentLabel.backgroundColor = UIColor.redColor()
        backgroundColor = UIColor.whiteColor()
        //  添加控件
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //  设置约束
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        userImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusTableViewCellMargin)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(userImageView.snp_bottom)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusTableViewCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
//            make.height.equalTo(80)
        }
        snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            self.originalViewBottom = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusTableViewCellMargin).constraint
            
        }
        
        
        
        
        
    }
    
    
}
